package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.Warehouse;

public class WarehousePresenter extends AbstractPresenter<Warehouse> {
	final String SINGLEITEM = "wearhouse";
	final String MULTIITEM = "wearhouses";
	
	public ArrayList<Warehouse> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Warehouse tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Warehouse tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			Warehouse item = createItemFromJSONObject(jsonHop);
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Warehouse createItemFromJSONObject(JSONObject jsonWH){
		Warehouse warehouse = new Warehouse();
		try{
			warehouse.setDescription(jsonWH.getString("WHDesc"));
			warehouse.setID(jsonWH.getInt("wearhouseID"));
			warehouse.setLocation(jsonWH.getString("WHLoc"));
			warehouse.setWHResaleable(jsonWH.getBoolean("WHResalable"));
			return warehouse;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Warehouse item){
		try{
			JSONObject json = new JSONObject();
			json.put("WHDesc", item.getDescription());
			json.put("WHLoc", item.getLocation());
			json.put("WHResalable", item.getWHResaleable());
			json.put("wearhouseID", item.getID());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Warehouse getEmptyEntity(){
		Warehouse entity = new Warehouse();
		return entity;
	}
}
